<?php
namespace app\api\controller;
use app\admin\model\Dis;
use app\admin\model\GiftList;
use app\admin\model\Locks;
use app\admin\model\MiningCoin;
use app\admin\model\MiningUserWallet;
use app\admin\model\UserLevelRule;
use app\admin\model\UserLevelRuleBzz;
use app\admin\model\UserMiner;
use app\admin\model\WaterLock;
use app\admin\model\WaterMining;
use app\admin\model\WaterPledge;

use app\common\model\Config;
use app\common\model\User;
use think\Log;
use think\Db;

class Crontab {



    /**
     * 获取汇率
     */
    public function getRate()
    {
        $data = file_get_contents(  "https://app.taibi.io/go-api/api/v1/markets/ticker_symbol?symbol=DMTC+/USDT");
        $data = json_decode($data,true);
        if (!empty($data['data']['ret']['ticker']['now_rate'])) {
            MiningCoin::where('id',2)->update(['price' => $data['data']['ret']['ticker']['now_rate']]);
        }else{
            Log::error('FPY Price get Fail');
        }
    }

    public function resetSystem()
    {

        $date = date('Y-m-d');
//        $date = "2021-2-17";
        $gift_data = UserMiner::whereTime('start_time','<=',$date)
            ->where('status',0)->where('type',3)->select();
        foreach ($gift_data as $value) {
            $user_id = $value->user_id;
            $pledge_cost = Config::where('name','gift_pledge')->value('value');
            $pledge_balance = UserMiner::where('user_id',$user_id)->whereIn('status',[0,1])->sum('pledge');
            WaterPledge::create([
                'detail_id' => $value->id,
//            'relate_user_id' => $to_user->id,
                'user_id' => $user_id,
                'type' => WATER_PLEDGE_PLEDGE,
                'money' => $pledge_cost,
                'balance' => $pledge_balance + $pledge_cost
            ]);
            UserMiner::where('id',$user_id)->setInc('pledge',$pledge_cost);
        }

            UserMiner::where('status',0)->whereTime('start_time','<=',$date)->update(['status' => 1]);
        UserMiner::whereTime('end_time','<=',$date)->update(['status' => 2]);
        GiftList::whereTime('end_time','<=',$date)->update(['status' => 2]);



        $data = UserMiner::whereIn('status',[0,1])->where('day_balance','<>',0)
            ->where('type',2)
//            ->whereIn('user_id',[131437,131419,131430,131423,131439])
            ->where('pledge_status',1)
            ->order('create_time','desc')->select();

        $t_fil = Config::where('name','t_fil')->value('value');
        $t_gas = Config::where('name','t_gas')->value('value');
        foreach ($data as $value)
        {

            $user_id = $value->user_id;
            $number = substr(sprintf("%.4f",($value->number - $value->balance_number) / $value->day_balance),0,-1);

            $pledge_cost = rounddown($number * $t_fil,6);

            $gas_cost = rounddown($number * $t_gas,6);

            $total_cost = $pledge_cost + $gas_cost;

            $user_fil_balance = MiningUserWallet::where('user_id',$value->user_id)
                ->where('mining_coin_id',COIN2_ID)->value('balance');
            if ($user_fil_balance < $total_cost) {
                continue;
            }

            MiningUserWallet::where('user_id',$user_id)
                ->where('mining_coin_id',COIN2_ID)->setDec('balance', $pledge_cost);
            WaterMining::create([
                'detail_id' => $value->id,
//            'relate_user_id' => $to_user->id,
                'user_id' => $user_id,
                'type' => MINING_WATER_PLEDGE,
                'coin_type' => COIN2,
                'money' => "-".$pledge_cost,
                'balance' => $user_fil_balance - $pledge_cost
            ]);

            $pledge_balance = UserMiner::where('user_id',$user_id)->whereIn('status',[0,1])->sum('pledge');
            WaterPledge::create([
                'detail_id' => $value->id,
//            'relate_user_id' => $to_user->id,
                'user_id' => $user_id,
                'type' => WATER_PLEDGE_PLEDGE,
                'money' => $pledge_cost,
                'balance' => $pledge_balance + $pledge_cost
            ]);
            UserMiner::where('id',$user_id)->setInc('pledge',$pledge_cost);

            //GAS
            $user_fil_balance = MiningUserWallet::where('user_id',$user_id)
                ->where('mining_coin_id',COIN2_ID)->value('balance');
            MiningUserWallet::where('user_id',$user_id)
                ->where('mining_coin_id',COIN2_ID)->setDec('balance',$gas_cost);
            WaterMining::create([
                'detail_id' => $value->id,
//            'relate_user_id' => $to_user->id,
                'user_id' => $user_id,
                'type' => MINING_WATER_PLEDGE_GAS,
                'coin_type' => COIN2,
                'money' => "-".$gas_cost,
                'balance' => $user_fil_balance - $gas_cost
            ]);

            UserMiner::where('id',$value->id)->update([
                'balance_number' => $value->balance_number + $number,
                'day_balance' => ($value->day_balance - 1),
                'pledge' => ($value->pledge + $pledge_cost),
                'gas' => $value->gas + $gas_cost
            ]);
        }

    }

    //激活BZZ宽带
    public function bzz_kd(){

        $time = date('Y-m-d H:i:s',time());
        
        //查询待挖矿的矿机
        $kj = Db::name("user_miner_ex_bzz")->where('end_time','<',$time)->select();

        if(!empty($kj)){

            $user_kj_list = array();

            foreach($kj as $k => $v){

                if (empty($user_kj_list[$v['user_miner_id']])) {
                    $user_kj_list[$v['user_miner_id']] = 0;
                }

                $user_kj_list[$v['user_miner_id']] ++;
            }

            if(!empty($user_kj_list)){

                foreach($user_kj_list as $k => $v){

                    //查询矿机
                    $user_miner = Db::name("user_miner")->where('id',$k)->find();

                    if($user_miner['balance_number'] > $v){

                        $res = Db::name("user_miner")->where('id',$k)->setDec('balance_number',$v);
                    }

                    if($user_miner['balance_number'] <= $v){

                        Db::name("user_miner")->where('id',$k)->update(['status' => 0,'balance_number' => 0]);
                    }
                }
            }

        }

        //查询待激活的宽带
        $kd = UserMiner::whereIn('status',[0,1])->where('ktype',4)->select();

        //获取当前时间
        $time = date('Y-m-d H:i:s',time());

        if(!empty($kd)){

            foreach($kd as $key => $value){

                if($value->status == 0){

                    //查询待挖矿的矿机
                    $kj = Db::name("user_miner_ex_bzz")->where('end_time','<',$time)->where('user_id',$value->user_id)->select();

                    $num = 0;
                    $num = $value->number;

                    if(!empty($kj)){

                        foreach($kj as $k => $v){

                            if($num > 0){

                                $end_time = date("Y-m-d",strtotime('+30 day'));

                                //更改结束时间
                                Db::name("user_miner_ex_bzz")->where('id',$v['id'])->update(['end_time' => $end_time]);

                                //更改激活数量
                                Db::name("user_miner")->where('id',$v['user_miner_id'])->setInc('balance_number',1);
                                Db::name("user_miner")->where('id',$v['user_miner_id'])->update(['status' => 1]);
                                Db::name("user_miner")->where('id',$value->id)->setInc('balance_number',1);
                                Db::name("user_miner")->where('id',$value->id)->update(['status' => 1]);

                                $num--;
                            }

                            if($num == 0){
                                break;
                            }
                        }
                    }
                }

                if($value->status == 1){

                    $sy = 0;
                    $sy = $value['number'] - $value['balance_number'];

                    if($sy <= 0){
                        $sy = 0;

                        //更改状态
                        UserMiner::where('id',$value->id)->update(['status' => 2]);
                    }else{

                        //查询待挖矿的矿机
                        $kj = Db::name("user_miner_ex_bzz")->where('end_time','<',$time)->where('user_id',$value->user_id)->select();

                        $num = 0;
                        $num = $sy;

                        if(!empty($kj)){

                            foreach($kj as $k => $v){

                                if($num > 0){

                                    $end_time = date("Y-m-d",strtotime('+30 day'));

                                    //更改结束时间
                                    Db::name("user_miner_ex_bzz")->where('id',$v['id'])->update(['end_time' => $end_time]);

                                    //更改激活数量
                                    Db::name("user_miner")->where('id',$v['user_miner_id'])->setInc('balance_number',1);
                                    Db::name("user_miner")->where('id',$v['user_miner_id'])->update(['status' => 1]);
                                    Db::name("user_miner")->where('id',$value->id)->setInc('balance_number',1);
                                    Db::name("user_miner")->where('id',$value->id)->update(['status' => 1]);

                                    $num--;
                                }

                                if($num == 0){
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        

        // foreach($kj as $key => $value){

        //     if (empty($user_kj_list[$value->user_id])) {
        //         $user_kj_list[$value->user_id] = 0;
        //     }

        //     $user_kj_list[$value->user_id] ++;
        // }

        // if(!empty($user_kj_list) && !empty($user_kd_list)){

        //     foreach($user_kj_list as $key => $value){

        //         foreach($user_kd_list as $k => $v){

        //             if($key == $k){


        //             }
        //         }
        //     }
        // }
                
    }

    //BZZ挖矿
     public function bzz_profit()
    {
        $total_profit = Dis::order('id','desc')->where('status',0)->where('ktype',2)->value('profit');
        if (!empty($total_profit)) {
            Dis::order('id','desc')->where('status',0)->where('ktype',2)->update(['status'=>1]);
            $miner_data = UserMiner::where('status',1)->where('ktype',2)
                //->whereIn('user_id',[131429,131428])
               // ->whereIn('user_id',[131437,131419,131430,131423,131439])
                ->select();
            $user_profit_list = [];
            $user_lock_list = [];
            $parent_profit_list = [];
            foreach ($miner_data as $value)
            {
                $user_id = $value->user_id;
                $service_rate = $value->service_rate;//服务费
                // $lock_rate = $value->lock_rate;//锁仓率
                $origin_profit = $total_profit * $value->balance_number;//每日分配 * 已激活算力

                $profit = $origin_profit * (1 - $service_rate);

                // $lock_profit = $profit * $lock_rate;
                // if (empty($user_lock_list[$value->user_id])) {
                //     $user_lock_list[$value->user_id] = 0;
                // }
                // $user_lock_list[$value->user_id] += $lock_profit;

                // $mining_profit = $profit * (1 - $lock_rate);
                $mining_profit = $profit;
                if (empty($user_profit_list[$value->user_id])) {
                    $user_profit_list[$value->user_id] = 0;
                }

                $user_profit_list[$value->user_id] += $mining_profit;

                var_dump($user_id);
                $parents_ids = getParents($user_id,'id,level_bzz');
                $level = 0;
                $tmp_level_rate = 0;
                $equal_level = 0;
                foreach ($parents_ids as $key => $parent) {
                    var_dump($parent->id);
                    $level_info = UserLevelRuleBzz::where('user_level_id',$parent->level_bzz)->find();
                    if (empty($level_info)) {
                        continue;
                    }
                    if ($parent->level_bzz > $level &&
                        !empty($level_info->mining_rate)
                    ) {

                        $team_profit = rounddown(($level_info->mining_rate - $tmp_level_rate)* $mining_profit / 100,6,PHP_ROUND_HALF_DOWN);
                        if (!empty($team_profit)) {
                            if (empty($parent_profit_list[$parent->id])) {
                                $parent_profit_list[$parent->id] = 0;
                            }
                            $parent_profit_list[$parent->id] += $team_profit;
                        }
                        $level = $level_info->user_level_id;
                        $tmp_level_rate = $level_info->mining_rate;
                    }
                }


            }

            foreach ($user_profit_list as $user_id => $profit) {
                if (!empty($profit)) {
                    $fil_balance = MiningUserWallet::where('user_id',$user_id)
                        ->where('mining_coin_id',COIN3_ID)->value('balance');
                    MiningUserWallet::where('user_id',$user_id)
                        ->where('mining_coin_id',COIN3_ID)->setInc('balance',$profit);
                    WaterMining::create([
                        'user_id' => $user_id,
                        'type' => MINING_WATER_MINING,
                        'ktype' => 2,
                        'coin_type' => COIN3,
                        'money' => $profit,
                        'balance' => $profit + $fil_balance
                    ]);
                }
            }

            // foreach ($user_lock_list as $user_id => $lock_profit) {
            //     if (!empty($lock_profit)) {
            //         $locks = User::where('id',$user_id)->value('locks');

            //         WaterLock::create([
            //             'user_id' => $user_id,
            //             'money' => $lock_profit,
            //             'balance' => $lock_profit  + $locks,
            //             'type' => 1,
            //         ]);
            //         User::where('id',$user_id)->setInc('locks', $lock_profit);
            //     }
            // }

            foreach ($parent_profit_list as $user_id => $profit) {
                $balance = MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN3_ID)
                    ->value('balance');
                MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN3_ID)
                    ->setInc('balance',$profit);
                WaterMining::create([
                    'user_id' => $user_id,
                    'type' => MINING_WATER_MINING_FENHONG,
                    'ktype' => 2,
                    'coin_type' => COIN3,
                    'money' => $profit,
                    'balance' => $balance + $profit
                ]);
            }

        }
    }

    /*
     *
     */
    public function profit()
    {
        $total_profit = Dis::order('id','desc')->where('status',0)->where('ktype',1)->value('profit');
        if (!empty($total_profit)) {
//            $total_cal = UserMiner::where('status',1)->sum('number');
            Dis::order('id','desc')->where('status',0)->where('ktype',1)->update(['status'=>1]);
            $miner_data = UserMiner::where('status',1)->where('ktype','<>',2)->where('ktype','<>',4)
//                ->whereIn('user_id',[131429,131428])
//                ->whereIn('user_id',[131437,131419,131430,131423,131439])
                ->select();
            $user_profit_list = [];
            $user_lock_list = [];
            $parent_profit_list = [];
            foreach ($miner_data as $value)
            {
                $user_id = $value->user_id;
                $service_rate = $value->service_rate;//服务费
                $lock_rate = $value->lock_rate;//锁仓率
                $origin_profit = $total_profit * $value->balance_number;//每日分配 * 已激活算力

                $profit = $origin_profit * (1 - $service_rate);

                $lock_profit = $profit * $lock_rate;
                if (empty($user_lock_list[$value->user_id])) {
                    $user_lock_list[$value->user_id] = 0;
                }
                $user_lock_list[$value->user_id] += $lock_profit;

                $mining_profit = $profit * (1 - $lock_rate);
                if (empty($user_profit_list[$value->user_id])) {
                    $user_profit_list[$value->user_id] = 0;
                }

                $user_profit_list[$value->user_id] += $mining_profit;

                var_dump($user_id);
                //分红：
                $parents_ids = getParents($user_id,'id,level');
                $level = 0;
//                $equal_level = 0;
                $tmp_level_rate = 0;
                $equal_level = 0;
                foreach ($parents_ids as $key => $parent) {
                    var_dump($parent->id);
                    $level_info = UserLevelRule::where('user_level_id',$parent->level)->find();
                    if (empty($level_info)) {
                        continue;
                    }
                    if ($parent->level > $level &&
                        !empty($level_info->mining_rate)
                    ) {
//                        if ($parent->id == 18) {
//                            var_dump($mining_profit);
//                            var_dump("dengcha:".$user_id."</br >");
//                        }
                        $team_profit = rounddown(($level_info->mining_rate - $tmp_level_rate)* $mining_profit / 100,6,PHP_ROUND_HALF_DOWN);
//                        var_dump($parent->id);
//                        var_dump($team_profit);
                        if (!empty($team_profit)) {
                            if (empty($parent_profit_list[$parent->id])) {
                                $parent_profit_list[$parent->id] = 0;
                            }
                            $parent_profit_list[$parent->id] += $team_profit;
                        }
                        $level = $level_info->user_level_id;
                        $tmp_level_rate = $level_info->mining_rate;
                    }
//                    elseif ($parent->level == $level &&
//                        !empty($level_info->equal_mining_rate) &&
//                        $parent->level > $equal_level
//                    ){
//                        if ($parent->id == 18) {
//                            var_dump($mining_profit);
//                            var_dump("pingji:".$user_id."</br >");
//                        }
//                        $equal_profit = rounddown($level_info->equal_mining_rate * $mining_profit / 100,6);
//                        if (!empty($equal_profit)) {
//                            if (empty($parent_profit_list[$parent->id])) {
//                                $parent_profit_list[$parent->id] = 0;
//                            }file_get_contents
//                            $parent_profit_list[$parent->id] += $equal_profit;
//                        }
//
//                        $equal_level = $level_info->user_level_id;
//                    }
                }


            }

            foreach ($user_profit_list as $user_id => $profit) {
                if (!empty($profit)) {
                    $fil_balance = MiningUserWallet::where('user_id',$user_id)
                        ->where('mining_coin_id',COIN2_ID)->value('balance');
                    MiningUserWallet::where('user_id',$user_id)
                        ->where('mining_coin_id',COIN2_ID)->setInc('balance',$profit);
                    WaterMining::create([
                        'user_id' => $user_id,
                        'type' => MINING_WATER_MINING,
                        'ktype' => 1,
                        'coin_type' => COIN2,
                        'money' => $profit,
                        'balance' => $profit + $fil_balance
                    ]);
                }
            }

            foreach ($user_lock_list as $user_id => $lock_profit) {
                if (!empty($lock_profit)) {
                    $locks = User::where('id',$user_id)->value('locks');

                    WaterLock::create([
                        'user_id' => $user_id,
                        'money' => $lock_profit,
                        'balance' => $lock_profit  + $locks,
                        'type' => 1,
//                        'address' => $value->name
                    ]);
                    User::where('id',$user_id)->setInc('locks', $lock_profit);
                }
            }

            foreach ($parent_profit_list as $user_id => $profit) {
                $balance = MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
                    ->value('balance');
                MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
                    ->setInc('balance',$profit);
                WaterMining::create([
//                        'detail_id' => $user_miner->id,
                    'user_id' => $user_id,
                    'type' => MINING_WATER_MINING_FENHONG,
                    'ktype' => 1,
                    'coin_type' => COIN2,
                    'money' => $profit,
                    'balance' => $balance + $profit
                ]);
            }

//            $pledge_rate = Config::where('name','pledge_rate')->value('value');
//            $service_rate = Config::where('name','service_rate')->value('value');
//            $lock_rate = Config::where('name','lock_rate')->value('value');     //苏仓率
//
//            $parent_profit_list = [];
//            foreach ($user_profit_list as $user_id => $profit)
//            {
//                $profit = $profit * (1 - $service_rate);            //服务费扣除。
//                var_dump($profit);
//                //锁仓
//                $lock_profit = $profit * $lock_rate;            //0.25
////                $lock_pledge_num = $lock_profit * $pledge_rate;     //锁仓需要质押的量
//
////                $lock_profit = $lock_profit * (1 - $pledge_rate);   //锁仓收益
//                $locks = User::where('id',$user_id)->value('locks');
//
//
//                WaterLock::create([
//                    'user_id' => $user_id,
//                    'money' => $lock_profit,
//                    'balance' => $lock_profit  + $locks,
//                    'type' => 1,
//                    'address' => $value->name
//                ]);
//                User::where('id',$user_id)->setInc('locks', $lock_profit);
//
//                $profit = $profit * (1 - $lock_rate);       //除锁仓外剩余的收益
//
//                //分红：
//                $parents_ids = getParents($user_id,'id,level');
//                $level = 0;
////                $equal_level = 0;
//                $tmp_level_rate = 0;
//                $equal_level = 0;
//                foreach ($parents_ids as $key => $parent) {
//                    $level_info = UserLevelRule::where('user_level_id',$parent->level)->find();
//                    if (empty($level_info)) {
//                        continue;
//                    }
//                    if ($parent->level > $level &&
//                        !empty($level_info->mining_rate)
//                    ) {
//                        $team_profit = rounddown(($level_info->mining_rate - $tmp_level_rate)* $profit / 100,4);
//                        if (!empty($team_profit)) {
//                            if (empty($parent_profit_list[$parent->id])) {
//                                $parent_profit_list[$parent->id] = 0;
//                            }
//                            $parent_profit_list[$parent->id] += $team_profit;
//                        }
//                        $level = $level_info->user_level_id;
//                        $tmp_level_rate = $level_info->mining_rate;
//                    } elseif ($parent->level == $level &&
//                        !empty($level_info->equal_mining_rate) &&
//                        $parent->level > $equal_level
//                    ){
//                        $equal_profit = rounddown($level_info->equal_mining_rate * $profit / 100,2);
//                        if (!empty($equal_profit)) {
//                            if (empty($parent_profit_list[$parent->id])) {
//                                $parent_profit_list[$parent->id] = 0;
//                            }
//                            $parent_profit_list[$parent->id] += $equal_profit;
//                        }
//
//                        $equal_level = $level_info->user_level_id;
//                    }
//                }
//
//
//                //挖矿
////                $user_miner = UserMiner::where('user_id',$user_id)->select();
////
////                $mining_pledge_profit = $profit * $pledge_rate;         //挖矿的质押
////
////                $balance_profit = $profit * (1 - $pledge_rate);      //
//
////                foreach ($user_miner as $value)
////                {
////                    if ($value->pledge < $value->max_pledge)
////                    {
////                        $dif = $value->max_pledge - $value->pledge;
////                        if ($mining_pledge_profit >= $dif) {
////                            $pledge_num += $dif;
////                            $mining_pledge_profit = $mining_pledge_profit - $dif;
////                            UserMiner::where('id',$value->id)->update(['pledge' => $value->max_pledge]);
////                        } else {
////                            $pledge_num += $mining_pledge_profit;
////                            UserMiner::where('id',$value->id)->setInc('pledge', $mining_pledge_profit);
////                            $mining_pledge_profit = 0;
////                            break;
////                        }
////                    }
////                }
//
////                if (!empty($pledge_num)) {
////                    $pledge_balance = UserMiner::where('user_id',$user_id)->whereIn('status',[0,1])->sum('pledge');
////                    WaterPledge::create([
//////                    'detail_id' => $user_miner->id,
////                        'user_id' => $user_id,
////                        'type' => WATER_PLEDGE_MINING,
////                        'money' => $pledge_num,
////                        'balance' => $pledge_balance + $pledge_num,
////                    ]);
////                    User::where('id',$user_id)->setInc('pledge',$pledge_num);
////                }
//
////                $balance_profit += $mining_pledge_profit;            //加上剩余的质押=总收益
//
//                if (!empty($profit)) {
//                    $fil_balance = MiningUserWallet::where('user_id',$user_id)
//                        ->where('mining_coin_id',COIN2_ID)->value('balance');
//                    MiningUserWallet::where('user_id',$user_id)
//                        ->where('mining_coin_id',COIN2_ID)->setInc('balance',$profit);
//                    WaterMining::create([
//                        'user_id' => $user_id,
//                        'type' => MINING_WATER_MINING,
//                        'coin_type' => COIN2,
//                        'money' => $profit,
//                        'balance' => $profit + $fil_balance
//                    ]);
//                }
//            }
//
//            foreach ($parent_profit_list as $user_id => $profit) {
//                $balance = MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
//                    ->value('balance');
//                MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)
//                    ->setInc('balance',$profit);
//                WaterMining::create([
////                        'detail_id' => $user_miner->id,
//                    'user_id' => $user_id,
//                    'type' => MINING_WATER_MINING_FENHONG,
//                    'coin_type' => COIN2,
//                    'money' => $profit,
//                    'balance' => $balance + $profit
//                ]);
//            }
        }
    }

    /**
     * 每日一次锁仓释放
     */
    public function lockRelease()
    {
        $exist = Locks::whereTime('create_time','today')->find();
        if (!empty($exist)) {
            return;
        }

        //统计昨天的挖矿加入锁仓
        $lock_data = WaterLock::field('sum(money) as lock_money,user_id')->where('type',1)
            ->whereTime('create_time','yesterday')
            ->where('id','>=',107)
//            ->where('id','<=',91)
            ->group('user_id')
            ->select();

        foreach ($lock_data as $value) {
            Locks::create([
                'user_id' => $value->user_id,
                'day' => 180,
                'day_balance' => 180,
                'money' => abs($value->lock_money),
                'balance' => 0,
                'day_release' => rounddown(abs($value->lock_money) / 180,6)
            ]);
        }

        $user_profit_list = [];
        $user_origin_list = [];
        $data = Locks::where('day_balance','<>',0)->select();
        foreach ($data as $value)
        {
            if (empty($user_profit_list[$value->user_id])) {
                $user_profit_list[$value->user_id] = 0;
                $user_origin_list[$value->user_id] = 0;
            }
            $user_profit_list[$value->user_id] += $value->day_release;
            $user_origin_list[$value->user_id] += $value->money;
            Locks::where('id',$value->id)->setInc('balance',$value->day_release);
            Locks::where('id',$value->id)->setDec('day_balance');
        }
//        var_dump($user_profit_list);exit;
//        $pledge_rate = Config::where('name','pledge_rate')->value('value');

        foreach ($user_profit_list as $user_id => $profit) {
            $user_balance = MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)->value('balance');
            $user_lock = User::where('id',$user_id)->value('locks');

            //挖矿
//            $user_miner = UserMiner::where('user_id',$user_id)->whereIn('status',[0,1])->select();

//            $mining_pledge_profit = $profit * $pledge_rate;         //挖矿的质押

//            $balance_profit = $profit * (1 - $pledge_rate);
//            $pledge_num = 0;
//            foreach ($user_miner as $value)
//            {
//                if ($value->pledge < $value->max_pledge)
//                {
//                    $dif = $value->max_pledge - $value->pledge;
//                    if ($mining_pledge_profit >= $dif) {
//                        $pledge_num += $dif;
//                        $mining_pledge_profit = $mining_pledge_profit - $dif;
//                        UserMiner::where('id',$value->id)->update(['pledge' => $value->max_pledge]);
//                    } else {
//                        $pledge_num += $mining_pledge_profit;
//                        UserMiner::where('id',$value->id)->setInc('pledge',$mining_pledge_profit);
//                        $mining_pledge_profit = 0;
//                        break;
//                    }
//                }
//            }

//            if (!empty($pledge_num)) {
//                $pledge_balance = UserMiner::where('user_id',$user_id)->whereIn('status',[0,1])->sum('pledge');
//                WaterPledge::create([
////                    'detail_id' => $user_miner->id,
//                    'user_id' => $user_id,
//                    'type' => WATER_PLEDGE_PLEDGE,
//                    'money' => $pledge_num,
//                    'balance' => $pledge_balance + $pledge_num,
//                ]);
//                User::where('id',$user_id)->setInc('pledge',$pledge_num);
//            }

//            $balance_profit += $mining_pledge_profit;            //加上剩余的质押=总收益

            if ($profit >= 0.00001) {
                WaterMining::create([
                    'user_id' => $user_id,
                    'origin_money' => $user_origin_list[$user_id],
                    'money' => $profit,
                    'balance' => $profit  + $user_balance,
                    'coin_type' => COIN2,
                    'type' => MINING_WATER_LOCK_RELEASE
                ]);
                MiningUserWallet::where('user_id',$user_id)->where('mining_coin_id',COIN2_ID)->setInc('balance',$profit);
//                User::where('id',$user_id)->setInc('balance',$balance_profit);

                WaterLock::create([
                    'user_id' => $user_id,
                    'money' => "-".$profit,
                    'balance' => $user_lock  - $profit,
                    'type' => 2
                ]);
                User::where('id',$user_id)->setDec('locks', $profit);
            }
        }
    }





}